const Core = require('electron').remote.require('./src/common/core.js')
const { remote } = require('electron')

class Win {
    constructor() {
        this.win = remote.getCurrentWindow()
        this.winInfo = Core.getWindowInfo(this.win.id)
    }

    /*
     * 通用弹窗函数
     */
    openWin(option) {
        option.fromWinId = this.win.id
        let win = Core.getFreeWindow(option)
        win.show()
        // 防止对象被销毁
        let winId = win.id
        return new Promise((resolve, reject) => {
            win.on('close', () => {
                let backMsg = Core.getWindowInfo(winId).backMsg
                resolve(backMsg)
            })
        }) 
    }

    /*
     * 关闭弹窗并且发送数据
     */
    closeWin(data) {
        if (data) {
            let _windowInfo = Core.getWindowInfo(this.win.id)
            _windowInfo.backMsg = data
            Core.setWindowInfo(_windowInfo, this.win.id)
        }
        this.win.close()
    }

    /*
     * 获取当前页面传递过来的参数
     */
    getParameter() {
        return this.winInfo.sendMsg
    }
}


module.exports = new Win()